<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\Circle;
use App\Models\Label;
use Illuminate\Http\Request;

class LabelController extends Controller
{
    //列表
    public function index(Request $request)
    {
        $data = Label::all();

        return $this->success($data);
    }

    public function status(Request $request)
    {
        $id = $request->post('id');
        $status = (Label::query()->where('id', $id)->value('status') == 1) ? 0 : 1;
        if ($id) {
            Label::query()->where('id', $id)->update(['status' => $status]);
            return $this->success();
        }
    }

    public function store(Request $request)
    {
        $data = $request->post();

        if ($data['title']) {
            $res = Label::query()->create($data);
            if ($res) {
                return $this->success();
            }
        }

        return $this->error();
    }

    public function show($id)
    {
        $data = Label::query()->find($id);
        if ($data) {
            return $this->success($data);
        }

        return $this->error();
    }

    public function update(Request $request, $id)
    {
        $data = $request->post();
        $res = Label::query()->where('id', $id)->update($data);

        if ($res) {
            return $this->success();
        }
        return $this->error();
    }

    //删除
    public function destroy($ids)
    {
        $ids = explode(',', $ids);

        $has = Circle::query()->whereIn('label_ids', $ids)->count();

        if ($has) {
            return $this->error('分类下有动态');
        }

        Label::query()->whereIn('id', $ids)->delete();

        return $this->success('', '删除成功');
    }
}
